﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using IDAL;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class UsersDAL : IUsersDAL
    {

        public static string Conns = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="UserTel"></param>
        /// <param name="UserPwd"></param>
        /// <returns></returns>
        public UserModel Login(string UserTel, string UserPwd)
        {
            using (SqlConnection sql = new SqlConnection(Conns))
            {
                sql.Open();
                string cmdStr = "select * from UserTable where UserTel='" + UserTel + "' and UserPwd='" + UserPwd + "'";
                SqlCommand cmd = new SqlCommand(cmdStr, sql);
                SqlDataAdapter dataSet = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                dataSet.Fill(table);
                UserModel user = new UserModel();
                if (table.Rows.Count != 0)
                {
                    user.UserID = Convert.ToInt32(table.Rows[0]["UserID"]);
                    user.UserTel = table.Rows[0]["UserTel"].ToString();
                    user.JurID = Convert.ToInt32(table.Rows[0]["JurID"]);
                    user.UserIntegral = Convert.ToInt32(table.Rows[0]["UserIntegral"]);
                    user.UserRegTime = Convert.ToDateTime(table.Rows[0]["UserRegTime"]);
                    user.UserState = Convert.ToInt32(table.Rows[0]["UserState"]);
                }
                else
                {
                    user = null;
                }
                return user;
            }
        }
        /// <summary>
        /// 注册方法
        /// </summary>
        /// <param name="tmodel"></param>
        /// <returns></returns>
        public int Regin(UserModel tmodel)
        {
            using (SqlConnection sql = new SqlConnection(Conns))
            {
                sql.Open();
                string cmdStr = "insert into UserTable(UserTel,UserPwd,JurID,UserState,UserIntegral,UserRegTime) values(@UserTel,@UserPwd,2,1,100,getdate())";
                SqlCommand cmd = new SqlCommand(cmdStr, sql);
                SqlParameter[] sp ={
                                      new SqlParameter("@UserTel",tmodel.UserTel),
                                      new SqlParameter("@UserPwd",tmodel.UserPwd)
                                  };
                cmd.Parameters.AddRange(sp);
                return cmd.ExecuteNonQuery();

            }

        }

        /// <summary>
        /// 检查是否存在当前手机号
        /// </summary>
        /// <param name="UserTel"></param>
        /// <returns></returns>
        public int checkd(string UserTel)
        {
            using (SqlConnection sql = new SqlConnection(Conns))
            {
                sql.Open();
                string cmdStr = "select count(1) from UserTable where UserTel=@UserTel";
                SqlCommand cmd = new SqlCommand(cmdStr, sql);
                cmd.Parameters.AddWithValue("@UserTel", UserTel);
                var ii = cmd.ExecuteScalar();
                return (int)ii;

            }
        }
    }
}
